package org.tinygame.herostory;

import io.netty.channel.Channel;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;

/**
 * 操作信道
 * @author shkstart
 * @create 2022-05-05 9:37
 */
public final class Broadcaster {
    /**
     * 客户端信道数组，一定要用static，否则不能群发
     */
    private static  final ChannelGroup _channelGroup=new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

    /**
     * 私有化类构造器
     */
    private Broadcaster(){

    }

    /**
     * 添加信道
     * @param channel
     */
    public static void addChannel(Channel channel){
        _channelGroup.add(channel);
    }

    /**
     * 移除信道
     * @param channel
     */
    public static void removeChannel(Channel channel){
        _channelGroup.remove(channel);
    }

    /**
     * 广播消息
     * @param msg
     */
    public static void broadcast(Object msg){
        if(null==msg){
            return;
        }
        _channelGroup.writeAndFlush(msg);
    }
}
